<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="账号">
          <el-input v-model="searchForm.name" clearable>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleFilter" class="search-button">查询</el-button>
          <el-button type="primary" @click="showAdd" class="add-button">添加</el-button>
          <el-button @click="handleReset" class="search-button">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" fit highlight-current-row>
        <el-table-column align="center" type="index" label="序号" width="55">
        </el-table-column>
        <el-table-column label="账户" align="center" prop="account">
        </el-table-column>
        <!-- <el-table-column label="角色" align="center" prop="roleName">
        </el-table-column> -->
        <el-table-column label="密码" align="center" prop="password">
        </el-table-column>
        <el-table-column label="用户名" align="center" prop="userName">
        </el-table-column>
        <el-table-column label="电话" align="center" prop="userPhone">
        </el-table-column>
        <el-table-column align="center" label="操作" width="200">
          <template slot-scope="{row}">
            <el-button @click="assignRoles(row)" type="text">分配角色</el-button>
            <el-button @click="showEdit(row)" type="text">编辑</el-button>
            <el-button @click="delHandle(row)" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="searchForm.pageIndex" :limit.sync="searchForm.pageSize"
        @pagination="getList" />
    </el-card>
    <el-dialog :title="dialogTit" :visible.sync="dialogVisible" width="40%">
      <el-form label-position="right" label-width="120px" ref="form" :model="form">
        <el-form-item label="账号">
          <el-input v-model.trim="form.account"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model.trim="form.password"></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model.trim="form.userName"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model.trim="form.userPhone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" class="btn-cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="分配角色" :visible.sync="roleDialog" width="40%">
      <el-form label-position="right" label-width="120px" ref="roleForm" :model="roleForm">
        <el-form-item label="角色">
          <el-select v-model="roleForm.roleIds" multiple placeholder="请选择" value-key="uid" clearable style="width: 100%;">
            <el-option v-for="item in rolsData" :key="item.uid" :label="item.roleName" :value="item.uid">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="roleDialog = false" class="btn-cancel">取 消</el-button>
        <el-button type="primary" @click="insertRole">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as account from '@/api/account'
import * as role from '@/api/role'
import Pagination from '@/components/Pagination'
export default {
  components: { Pagination },
  data () {
    return {
      list: null,
      listLoading: true,
      searchForm: {
        pageIndex: 1,
        pageSize: 10
      },
      total: 0,
      dialogVisible: false,
      roleDialog: false,
      dialogTit: '',
      form: {},
      roleForm: {},
      rolsData: []
    }
  },
  created () {
    this.getList()
    this.getRoles()
  },
  methods: {
    getList () {
      this.listLoading = true
      let param = Object.assign({}, this.searchForm)
      account.queryByPage(param).then(response => {
        if (response.code == 200) {
          this.list = response.data.rows
          this.total = response.data.total
          this.listLoading = false
        }
      })
    },
    getRoles () {
      role.queryByPage({}).then(response => {
        if (response.code == 200) {
          this.rolsData = response.data.rows
        }
      }).catch(error => {
        console.log(error)
      })
    },
    handleFilter () {
      this.searchForm.pageIndex = 1
      this.getList()
    },
    handleReset () {
      this.resetTemp();
      this.getList();
    },
    resetTemp () {
      this.searchForm = {
        pageIndex: 1,
        pageSize: 10
      }
    },
    showAdd () {
      this.dialogTit = '新增'
      this.form = {}
      this.dialogVisible = true
    },
    showEdit (row) {
      this.dialogTit = '编辑'
      this.form = Object.assign({}, row)
      delete this.form.createTime
      delete this.form.updateTime
      this.dialogVisible = true
    },
    submit () {
      if (this.dialogTit === '新增') {
        this.creatHandle()
      } else if (this.dialogTit === '编辑') {
        this.editHandle()
      }
    },
    creatHandle () {
      account.insert(this.form).then(response => {
        if (response.code == 200) {
          this.dialogVisible = false
          this.getList()
          this.$message.success('新增成功')
        }
      }).catch(error => {
        console.log(error)
      })
    },
    editHandle () {
      account.update(this.form).then(response => {
        if (response.code == 200) {
          this.dialogVisible = false
          this.getList()
          this.$message.success('修改成功')
        }
      }).catch(error => {
        console.log(error)
      })
    },
    delHandle (data) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let param = {
          uid: data.uid,
        }
        account.deleteById(param).then(result => {
          this.$message.success(result.msg)
          this.getList()
        }).catch(error => {
          this.loading = false
          console.log(error)
        })
      }).catch(() => {
        console.log('取消删除')
      });
    },
    insertRole () {
      let param = {
        userId: this.form.userId,
        roleIds: this.roleForm.roleIds
      }
      role.roleInsert(param).then(response => {
        if (response.code == 200) {
          this.roleDialog = false
          this.getList()
        }
      }).catch(error => {
        console.log(error)
      })
    },
    assignRoles (row) {
      role.getRole({ userId: row.uid }).then(response => {
        let arr = []
        if (response.code == 200) {
          response.data.rows.forEach(item => {
            arr.push(item.uid)
          })
        }
        this.roleForm.roleIds = arr
      }).catch(error => {
        console.log(error)
      })
      this.form.userId = row.uid
      this.roleDialog = true
    }
  }
}
</script>
